/**
 * Copyright (c) 2020-2024, huli.com
 * All rights reserved.
 */
package com.xiaohujr.xuexue.leetcode;

import java.util.*;

/**
 * @author Xue Xue (xuexue1@huli.com)
 * @version 1.0
 * @since 2021/3/18
 */
public class PriorityQueueTest {

    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : nums) {
            if (map.get(i) == null) {
                map.put(i, 1);
            } else {
                map.put(i, map.get(i) + 1);
            }
        }
        List<Integer>[] list = new ArrayList[nums.length + 1];
        for (int key : map.keySet()) {
            List<Integer> sub = new ArrayList<>();
            if (list[map.get(key)] != null) {
                sub = list[map.get(key)];
            } else {
                list[map.get(key)] = sub;
            }
            sub.add(key);
        }
        List<Integer> ansList = new ArrayList<>();
        for (int i = list.length - 1; i >= 0; i--) {
            if (list[i] != null && !list[i].isEmpty()) {
                ansList.addAll(list[i]);
            }
        }
        int[] ans = new int[k];
        for (int i = 0; i < k; i++) {
            ans[i] = ansList.get(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        Queue test = new PriorityQueue<Integer>();
        test.offer(3);
        test.offer(4);
        test.offer(1);
//        Iterator iterator = new ArrayList<Integer>().iterator();
        new PriorityQueueTest().topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2);

    }
}
